//
//  ViewController.m
//  04 Control Fun
//
//  Created by willter on 13-1-18.
//  Copyright (c) 2013年 willter. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

// return键退出软键盘
- (IBAction)textFieldDoneEditing:(id)sender {
    [sender resignFirstResponder];
}
// 点击屏幕任意位置退出软键盘
- (IBAction)backgroundTap:(id)sender {
    [self.txtName resignFirstResponder];
    [self.txtNumber resignFirstResponder];
}
// 滑动标签改变Label的值
- (IBAction)sliderChanged:(id)sender {
    UISlider *slider = (UISlider *)sender;
    int progressAsInt = (int)(slider.value + 0.5f);
    NSString *newText = [NSString stringWithFormat:@"%d", progressAsInt];
    NSLog(@"%@", newText);
    self.lblSlider.text = newText;
}
// Segmented Control控件值改变事件触发方法
- (IBAction)toggleControls:(id)sender {
    if ([sender selectedSegmentIndex] == kSwitchesSegmentIndex) {
        self.leftSwitch.hidden = NO;
        self.rightSwitch.hidden = NO;
        self.btnDoSome.hidden = YES;
    } else {
        self.leftSwitch.hidden = YES;
        self.rightSwitch.hidden = YES;
        self.btnDoSome.hidden = NO;
    }
}
// Switch控件值改变事件触发方法
- (IBAction)switchChanged:(id)sender {
    UISwitch *whichSwitch = (UISwitch *)sender;
    BOOL setting = whichSwitch.isOn;
    [self.leftSwitch setOn:setting animated:YES];
    [self.rightSwitch setOn:setting animated:YES];
}
// btnDoSome按钮按下时触发事件
- (IBAction)buttonPressed:(id)sender {
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:@"Are you sure?"
                                  delegate:self
                                  cancelButtonTitle:@"No Way!"
                                  destructiveButtonTitle:@"Yes, I'm Sure!"
                                  otherButtonTitles:nil];
    [actionSheet showInView:self.view];
    [actionSheet release];
}

- (void)actionSheet:(UIActionSheet *)actionSheet
didDismissWithButtonIndex:(NSInteger)buttonIndex {
    // 判断按下的是否取消按钮
    if (buttonIndex != [actionSheet cancelButtonIndex]) {
        NSString *msg = nil;
        
        if (self.txtName.text.length > 0) {
            msg = [[NSString alloc] initWithFormat:@"You can breathe easy, %@, everything went OK.", self.txtName.text];
        } else {
            msg = @"You can breathe easy, everything went OK.";
        }
        
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Something was done"
                              message:msg
                              delegate:self
                              cancelButtonTitle:@"Phew!"
                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        [msg release];
    }
}

// 界面初始化调用
- (void)viewDidLoad
{
    // 设置btnDoSome按钮的样式，同时需要将Round Rect修改为Custom
    UIImage *buttonImageNormal = [UIImage imageNamed:@"whiteButton.png"];
    UIImage *stretchableButtonImageNormal = [buttonImageNormal
                                             stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [self.btnDoSome setBackgroundImage:stretchableButtonImageNormal
                              forState:UIControlStateNormal];
    
    UIImage *buttonImagePressed = [UIImage imageNamed:@"blueButton.png"];
    UIImage *stretchableButtonImagePressed = [buttonImagePressed
                                              stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [self.btnDoSome setBackgroundImage:stretchableButtonImagePressed
                              forState:UIControlStateHighlighted];
    //[super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    self.txtName = nil;
    self.txtNumber = nil;
    self.lblSlider = nil;
    self.leftSwitch = nil;
    self.rightSwitch = nil;
    self.btnDoSome = nil;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [self.txtName release];
    [self.txtNumber release];
    [self.lblSlider release];
    [self.leftSwitch release];
    [self.rightSwitch release];
    [self.btnDoSome release];
    [super dealloc];
}
@end
